package gui.beans;


import java.awt.*;

import javax.swing.*;
import javax.swing.event.*;

import datos.Incidencia;
import datos.Tarea;

import java.util.*;
import java.util.List;

@SuppressWarnings("serial")
public class MyCheckBoxJList extends JList implements ListSelectionListener {

	static Color listForeground, listBackground, listSelectionForeground, listSelectionBackground;
	static {
		UIDefaults uid = UIManager.getLookAndFeel().getDefaults();
		listForeground = uid.getColor("List.foreground");
		listBackground = uid.getColor("List.background");
		listSelectionForeground = uid.getColor("List.selectionForeground");
		listSelectionBackground = uid.getColor("List.selectionBackground");
	}

	HashSet<Integer> selectionCache = new HashSet<Integer>();
	int toggleIndex = -1;
	boolean toggleWasSelected;

	private boolean soloActivas = false;
	
	public MyCheckBoxJList(ModeloListaTareas modelo) {
		super(modelo);
		setCellRenderer(new CheckBoxListCellRenderer());
		addListSelectionListener(this);
	}

	public void setSoloActivas(boolean flag){
		soloActivas = flag;
	}
	
	public List<Tarea> getCheckValues(){
		List<Tarea> l = new ArrayList<Tarea>();
		ModeloListaTareas modelo = (ModeloListaTareas)this.getModel();
		Iterator<Integer> i = selectionCache.iterator();
		while(i.hasNext()){
			l.add(modelo.get(i.next().intValue()));
		}
		return l;		
	}
	
	// ListSelectionListener implementation
	public void valueChanged(ListSelectionEvent lse) {
		if (!lse.getValueIsAdjusting()) {
			removeListSelectionListener(this);

			// remember everything selected as a result of this action
			HashSet<Integer> newSelections = new HashSet<Integer>();
			int size = getModel().getSize();
			for (int i = 0; i < size; i++) {
				if (getSelectionModel().isSelectedIndex(i)) {
					newSelections.add(new Integer(i));
				}
			}

			// turn on everything that was previously selected
			Iterator<Integer> it = selectionCache.iterator();
			while (it.hasNext()) {
				int index = it.next().intValue();
				getSelectionModel().addSelectionInterval(index, index);
			}

			// add or remove the delta
			it = newSelections.iterator();
			while (it.hasNext()) {
				Integer nextInt = it.next();
				int index = nextInt.intValue();
				if (selectionCache.contains(nextInt))
					getSelectionModel().removeSelectionInterval(index, index);
				else
					getSelectionModel().addSelectionInterval(index, index);
			}

			// save selections for next time
			selectionCache.clear();
			for (int i = 0; i < size; i++) {
				if (getSelectionModel().isSelectedIndex(i)) {
					selectionCache.add(new Integer(i));
				}
			}

			addListSelectionListener(this);

		}
	}

	class CheckBoxListCellRenderer extends JComponent implements ListCellRenderer {
		DefaultListCellRenderer defaultComp;
		JCheckBox checkbox;

		public CheckBoxListCellRenderer() {
			setLayout(new BorderLayout());
			defaultComp = new DefaultListCellRenderer();
			checkbox = new JCheckBox();
			add(checkbox, BorderLayout.WEST);
			add(defaultComp, BorderLayout.CENTER);
		}

		public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
			Tarea inc = (Tarea) value;
			
			if (soloActivas && !inc.isActiva())
					return new JLabel();
			
			Component c = defaultComp.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
			
			/*
			 * checkbox.setSelected (isSelected); checkbox.setForeground
			 * (isSelected ? listSelectionForeground : listForeground);
			 * checkbox.setBackground (isSelected ? listSelectionBackground :
			 * listBackground);
			 */
			checkbox.setSelected(isSelected);
			Component[] comps = getComponents();
			for (int i = 0; i < comps.length; i++) {
				comps[i].setForeground(listForeground);
				comps[i].setBackground(listBackground);
			}
			
			if (inc.getEstado().equals(Incidencia.CONGELADA))
				c.setForeground(Color.BLUE.darker());
			if (inc.getEstado().equals(Incidencia.PRUEBAS))
				c.setForeground(Color.GREEN.darker());
			if (inc.getEstado().equals(Incidencia.CERRADA))
				c.setForeground(Color.RED.darker());
			return this;
		}
	}
}
